//
//  ToastView.m
//  MeicaiCrm
//
//  Created by zhou jianbang on 2019/4/16.
//  Copyright © 2019 meicai. All rights reserved.
//

#import "ToastView.h"
#import <MCFoundation/MCFoundation.h>
#import <Masonry/Masonry.h>

@interface ToastView()

@property (nonatomic, strong) UIView * contentView;
@property (nonatomic, strong) UILabel * tipLabel;
@property (nonatomic, strong) UIImageView * tipImg;
@property (nonatomic, strong) UIActivityIndicatorView * activity;

@end

@implementation ToastView

-(id)initWithDataDict:(NSDictionary *)dict;
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor mcColorWithHexString:@"#3A3A3A"];
        self.layer.cornerRadius = 2;
        self.clipsToBounds = YES;
        [self matchData:dict];
    }
    return self;
}
-(void)matchData:(NSDictionary *)dict
{
    NSString * type = @"info";
    if([dict mcObjectForKey:@"type"]){
        type = [dict mcObjectForKey:@"type"];
    }
    NSString * msg = [dict mcObjectForKey:@"msg"];
    if([type isEqualToString:@"info"]){
        [self configUIHasTip:msg];
    }else if([type isEqualToString:@"error"]){
        [self configUIHasImg:@"icon_error" andMsg:msg];
    }else if([type isEqualToString:@"success"]){
        [self configUIHasImg:@"icon_success" andMsg:msg];
    }else if([type isEqualToString:@"warning"]){
        [self configUIHasImg:@"icon_warning" andMsg:msg];
    }else if([type isEqualToString:@"loading"]){
        [self configUIHasLoading:msg];
    }
}
-(void)configUIHasImg:(NSString *)imgName andMsg:(NSString *)msg
{
    [self addSubview:self.tipLabel];
    [self addSubview:self.tipImg];
    [self.tipImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25);
        make.centerX.mas_equalTo(self.mas_centerX);
        make.height.mas_equalTo(36);
        make.width.mas_equalTo(36);
    }];
    [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tipImg.mas_bottom).offset(8);
        make.centerX.mas_equalTo(self.mas_centerX);
    }];
    self.tipLabel.text = msg;
    self.tipImg.image = [UIImage imageNamed:imgName];
}
-(void)configUIHasTip:(NSString *)msg
{
    [self addSubview:self.tipLabel];
    [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.lessThanOrEqualTo(@(150));
        make.left.offset(10);
        make.right.offset(-10);
        make.top.offset(10);
        make.bottom.offset(-10);
    }];
    self.tipLabel.text = msg;
}
-(void)configUIHasLoading:(NSString *)msg
{
    [self addSubview:self.tipLabel];
    [self addSubview:self.activity];
    [self.activity mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25);
        make.centerX.mas_equalTo(self.mas_centerX);
        make.height.mas_equalTo(36);
        make.width.mas_equalTo(36);
    }];
    [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.activity.mas_bottom).offset(8);
        make.centerX.mas_equalTo(self.mas_centerX);
    }];
    self.tipLabel.text = msg;
}

-(void)startLoading
{
    [self.activity startAnimating];
}
-(void)stopLoading
{
    [self.activity stopAnimating];
}

-(UILabel *)tipLabel
{
    if (!_tipLabel) {
        _tipLabel = [[UILabel alloc]init];
        _tipLabel.textColor = [UIColor whiteColor];
        _tipLabel.font = [UIFont systemFontOfSize:14];
        _tipLabel.numberOfLines = 0;
    }
    return _tipLabel;
}
-(UIImageView *)tipImg
{
    if (!_tipImg) {
        _tipImg = [[UIImageView alloc]init];
    }
    return _tipImg;
}
-(UIActivityIndicatorView *)activity
{
    if (!_activity) {
        _activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    }
    return _activity;
}
@end
